﻿using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Nx.Services
{
    public class BindingFactory : Singleton<BindingFactory>, IBindingFactory
    {
        public BindingFactory()
        {
        }

        ~BindingFactory()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
            }
        }

        public Binding CreateHttpBinding()
        {
            BindingElementCollection bindingElements = new BindingElementCollection(
                new BindingElement[] { 
                    new TextMessageEncodingBindingElement(),
                    new HttpTransportBindingElement()
                });
            return CreateCustomBinding(bindingElements);
        }

        public Binding CreateHttpBinaryBinding()
        {
            BindingElementCollection bindingElements = new BindingElementCollection(
                new BindingElement[] { 
                    new BinaryMessageEncodingBindingElement(),
                    new HttpTransportBindingElement()
                });
            return CreateCustomBinding(bindingElements);
        }

        public Binding CreateCustomBinding(BindingElementCollection bindingElementCollection)
        {
            CustomBinding binding = new CustomBinding();
            binding.Elements.AddRange(bindingElementCollection.ToArray());
            return binding;
        }

        public Binding CreateNetNamedPipeBinding()
        {
            return new NetNamedPipeBinding();
        }
    }
}
